package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.BaseContext;
import com.example.entity.OrderDetail;
import com.example.entity.ShoppingCart;
import com.example.mapper.ShoppingCartMapper;
import com.example.service.IShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 购物车 服务实现类
 * </p>
 *
 * @author czx
 * @since 2022-04-28
 */
@Service
@Slf4j
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements IShoppingCartService {


    /**
     * 添加购物车
     *
     * @param shoppingCart 购物车
     * @return 购物车
     */
    @Override
    public ShoppingCart saveWithDishOrSetmeal(ShoppingCart shoppingCart) {
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        shoppingCart.setCreateTime(LocalDateTime.now());

        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();
        String dishFlavor = shoppingCart.getDishFlavor();

        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(ShoppingCart::getUserId, currentId);

        //查询是否为菜品id或套餐id
        wrapper.eq(dishId != null, ShoppingCart::getDishId, dishId);
        wrapper.eq(setmealId != null, ShoppingCart::getSetmealId, setmealId);
        // 是否有菜品口味
        wrapper.eq(StringUtils.isNotEmpty(dishFlavor), ShoppingCart::getDishFlavor, dishFlavor);

        ShoppingCart one = this.getOne(wrapper);
        //如果购物车该套餐或菜品不为空
        if (one != null) {
            // 将购物车的套餐或菜品数量加1
            Integer number = 1 + one.getNumber();
            one.setNumber(number);
            this.updateById(one);
        } else {
            //不存在则数量设置为1并添加到购物车
            shoppingCart.setNumber(1);
            this.save(shoppingCart);
            //将one赋值
            one = shoppingCart;
        }
        return one;
    }

    /**
     * 清空购物车
     *
     * @return 清空结果
     */
    @Override
    public boolean clean() {
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        Long userId = BaseContext.getCurrentId();
        // 清空购物车
        wrapper.eq(userId != null, ShoppingCart::getUserId, userId);
        return this.remove(wrapper);
    }

    /**
     * 数量 -1
     *
     * @param shoppingCart 购物车
     * @return 减一结果
     */
    @Override
    public boolean sub(ShoppingCart shoppingCart) {
        LambdaUpdateWrapper<ShoppingCart> wrapper = new LambdaUpdateWrapper<>();
        // 用户id
        wrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        // 判断是套餐还是菜品
        wrapper.eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId());
        wrapper.eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        //判断是否有菜品口味
        wrapper.eq(StringUtils.isNotEmpty(shoppingCart.getDishFlavor()),ShoppingCart::getDishFlavor,shoppingCart.getDishFlavor());
        // 从购物车查找是否存在
        shoppingCart = this.getOne(wrapper);
        if (shoppingCart != null) {
            Integer number = shoppingCart.getNumber();
            // 如果数量为1则删除
            if(number==1){
                return this.remove(wrapper);
            }
            // 数量减一
            wrapper.set(ShoppingCart::getNumber, number - 1);
            // 要传的是wrapper
            return this.update(wrapper);
        }
        return false;
    }

    /**
     * 根据用户id进行查询
     *
     * @param userId 用户id
     * @return 该用户的购物车列表信息
     */
    @Override
    public List<ShoppingCart> getByUserId(Long userId) {
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, userId);
        return this.list(wrapper);
    }

    /**
     * 将再来一单的数据加入到购物车
     *
     * @param orderDetailList 订单明细列表
     * @return 结果
     */
    @Override
    public boolean saveWithOrderDetail(List<OrderDetail> orderDetailList) {
        List<ShoppingCart> shoppingCarts = this.changeWithOrderDetail(orderDetailList);
        return this.saveBatch(shoppingCarts);
    }

    /**
     * 查询购物车
     *
     * @return 购物车信息
     */
    @Override
    public List<ShoppingCart> list() {
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        log.info("查看购物车：{}",BaseContext.getCurrentId());
        wrapper.orderByDesc(ShoppingCart::getCreateTime);
        return this.list(wrapper);
    }

    /**
     * 讲订单明细转换成购物车
     *
     * @param orderDetailList 订单明细列表
     * @return 购物车列表
     */
    private List<ShoppingCart> changeWithOrderDetail(List<OrderDetail> orderDetailList){
        return orderDetailList.stream().map((item) -> {
            ShoppingCart shoppingCart = new ShoppingCart();
            shoppingCart.setName(item.getName());
            shoppingCart.setImage(item.getImage());
            shoppingCart.setDishId(item.getDishId());
            shoppingCart.setSetmealId(item.getSetmealId());
            shoppingCart.setDishFlavor(item.getDishFlavor());
            shoppingCart.setNumber(item.getNumber());
            shoppingCart.setAmount(item.getAmount());
            // 订单明细没有用户id和创建时间
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCart.setCreateTime(LocalDateTime.now());
            return shoppingCart;
        }).collect(Collectors.toList());
    }
}
